package com.cjm.algorithm.leetcode;

import java.util.HashMap;
import java.util.Map;

public class Ti003 {
    public static void main(String[] args) {

    }

    public int lengthOfLongestSubstring1(String s) {
        Map<Character, Integer> map = new HashMap<>();
        int ans = 0;
        for (int start = 0, end = 0; end < s.length(); end++) {
            char cur = s.charAt(end);
            map.put(cur, map.getOrDefault(cur, 0) + 1);
            while (map.get(cur) > 1) {
                char c = s.charAt(start);
                map.put(c, map.get(c) - 1);
                start++;
            }
            ans = Math.max(ans, end - start + 1);
        }
        return ans;
    }

    public int lengthOfLongestSubstring(String s) {
        // 最长无重复子串
        Map<Character, Integer> count = new HashMap<>();
        int left = 0;
        int ans = 0;
        char[] charArray = s.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            char abc = charArray[i];
            count.put(abc, count.getOrDefault(abc, 0) + 1);
            while (count.get(abc) > 1) {
                count.put(abc, count.get(abc) - 1);
                left++;
            }
            ans = Math.max(ans, i - left);
        }
        return ans;
    }
}
